package com.example.ui_mvc

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.graphics.Color
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    private lateinit var programAdviser: ProgramAdviser

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 功能区1：动态生成TextView
        val btnAdd: Button = findViewById(R.id.btn_add)
        val etInput: EditText = findViewById(R.id.et_input)
        val container: LinearLayout = findViewById(R.id.container_dynamic_text)

        btnAdd.setOnClickListener {
            val inputText = etInput.text.toString()
            if (inputText.isNotEmpty()) {
                val newTextView = TextView(this).apply {
                    text = inputText
                    layoutParams = LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                    ).apply {
                        setMargins(0, 8, 0, 8)
                    }
                    setPadding(20, 15, 20, 15)
                    setBackgroundColor(Color.LTGRAY)
                    textSize = 16f
                }
                container.addView(newTextView)
                etInput.text.clear()
            } else {
                Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show()
            }
        }

        // 功能区2：MVC模式的专业推荐
        programAdviser = ProgramAdviser()  // 第58行 - 需要创建ProgramAdviser类

        val btnQuery: Button = findViewById(R.id.btn_query_advice)
        val tvResult: TextView = findViewById(R.id.tv_advice_result)

        btnQuery.setOnClickListener {
            val advice = programAdviser.getAdvice()
            // 第72行 - 使用字符串资源替代直接拼接
            tvResult.text = getString(R.string.program_advice, advice)
        }
    }
}